<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('流程标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('流程标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('名字')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('名字')" show-word-limit maxlength="100">
              <template slot="append">
                <a @click="selectDataList">{{ $t('选择转正') }}</a>
              </template>
            </el-input>

          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('证件类型')" prop="idType">
            <dictSelect
              v-model="form.idType"
              :clearable="true"
              dictionary="hr_id_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('证件号码')" prop="idNum">
            <el-input v-model="form.idNum" :placeholder="$t('证件号码')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('性别')" prop="sex">
            <dictSelect
              v-model="form.sex"
              :clearable="true"
              dictionary="sys_user_sex"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('年龄')" prop="age">
            <el-input
              v-model="form.age"
              :placeholder="$t('年龄')"
              oninput="value=value.replace(/[^\d]/g,'')"
              show-word-limit
              maxlength="10"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('学历')" prop="education">
            <el-input v-model="form.education" :placeholder="$t('学历')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('学校')" prop="school">
            <el-input v-model="form.school" :placeholder="$t('学校')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('专业')" prop="major">
            <el-input v-model="form.major" :placeholder="$t('专业')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('到职日期')" prop="entryTime">
            <el-date-picker
              v-model="form.entryTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('到职日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实际部门')" prop="deptId">
            <DeptSelect v-model="form.deptId" :placeholder="$t('实际部门')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('实际职位')" prop="postId">
            <el-select v-model="form.postId" multiple :placeholder="$t('请选择')">
              <el-option
                v-for="item in postOptions"
                :key="item.postId"
                :label="$t(item.postName)"
                :value="item.postId"
              />
            </el-select>
          </el-form-item>

        </el-col>

        <el-col :span="12">
          <el-form-item :label="$t('薪资福利（月薪）') + $t('金额单位')" prop="salary">
            <money v-model="form.salary" :placeholder="$t('薪资福利（月薪）')+$t('金额单位')" unit="w" @change="changeSalary" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合同期限')" prop="contractTerm">
            <dictSelect
              v-model="form.contractTerm"
              :clearable="true"
              dictionary="hr_contract_period"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('试用期')" prop="probation">
            <dictSelect
              v-model="form.probation"
              :clearable="true"
              dictionary="hr_trial_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('试用期薪资（月薪）') + $t('金额单位')" prop="probationSalary">
            <money v-model="form.probationSalary" :placeholder="$t('薪资福利（月薪）') + $t('金额单位')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('转正日期')" prop="becomeTime">
            <el-date-picker
              v-model="form.becomeTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('转正日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('对工作总结')" prop="summary">
            <el-input
              v-model="form.summary"
              type="textarea"
              :placeholder="$t('对工作总结')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('工作建议')" prop="proposal">
            <el-input
              v-model="form.proposal"
              type="textarea"
              :placeholder="$t('工作建议')"
              show-word-limit
              maxlength="1000"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!--文件-->
      <FileTable :list.sync="form.fileList" :data-type="dataType" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="hr:become" :menu-key="menuKey" />
    </div>
    <!-- 预览附件 -->
    <OnlinePreview ref="OnlinePreview" />

    <!-- 添加或修改入职管理对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogNew"
      append-to-body
    >
      <hrEntrySelectList ref="hrEntrySelectList" />
    </el-dialog>
  </div>
</template>

<script>
import { saveHrBecomeWork, getHrBecomeWork } from '@/api/oa/hr/hrBecomeWork'
import { listPost } from '@/api/system/post'
import hrEntrySelectList from '@/views/oa/hr/entry/hrEntrySelectList'

export default {
  components: {
    hrEntrySelectList
  },
  data() {
    return {
      // 菜单标识
      menuKey: this.$t('hr_become'),
      formOpen: false,
      title: '选择转正',
      otherBtnHide: false,
      dataType: 'hr_become',
      // 表单参数
      form: {
        fileList: []
      },
      // 岗位选项
      postOptions: [],
      // 表单校验
      rules: {
        actTitle: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], name: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], idType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], idNum: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], sex: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], age: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], education: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], major: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], entryTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], deptId: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], postId: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], contractTerm: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], probation: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], probationSalary: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], becomeTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], summary: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      // 遮罩层
      loading: false,
      entry: {},
      entryId: undefined,
      // 操作按钮默认不展示
      buttonShow: true
    }
  },
  watch: {
    'entry.id': {
      deep: true, // 深度监听设置为 true
      handler: function(val) {
        if (val !== null && val !== undefined && val !== '') {
          this.form.entryId = this.entry.id
          this.form.name = this.entry.name
          this.form.idType = this.entry.idType
          this.form.idNum = this.entry.idNum
          this.form.sex = this.entry.sex
          this.form.age = this.entry.age
          this.form.education = this.entry.education
          this.form.school = this.entry.school
          this.form.major = this.entry.major
          this.form.major = this.entry.major
          this.form.entryTime = this.entry.entryTime
          this.form.deptId = this.entry.deptId
          this.form.postId = [this.entry.postId]
          this.form.salary = this.entry.salary
          this.form.contractTerm = this.entry.contractTerm
          this.form.probation = this.entry.probation
          this.form.probationSalary = this.entry.probationSalary
        }
      }
    }
  },
  created() {
    this.initDict()
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getHrBecomeWork(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        actId: undefined,
        actStatus: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        actTitle: undefined,
        name: undefined,
        idType: undefined,
        idNum: undefined,
        sex: undefined,
        age: undefined,
        education: undefined,
        school: undefined,
        major: undefined,
        entryTime: undefined,
        deptId: undefined,
        postId: undefined,
        salary: undefined,
        contractTerm: undefined,
        probation: undefined,
        probationSalary: undefined,
        becomeTime: undefined,
        summary: undefined,
        proposal: undefined,
        entryId: undefined,
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveHrBecomeWork(this.form).then(response => {
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  // 启动流程
                  this.startProcess(response.data)
                  // 点击的仅保存
                } else {
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm()
                }
                resolve()
              } else {
                reject()
                this.msgError(response.msg)
                this.loading = false
              }
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: function(dataId) {
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = this.form.actTitle
      // 业务表名
      processData.dataTable = 'hr_become_work'
      // 业务ID
      processData.dataId = dataId.id
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/oa/hr/become/hrBecomeWorkForm'
      processData.viewPath = '@/views/oa/hr/become/hrBecomeWorkView'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = this.menuKey
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    },
    changeSalary() {
      this.form.probationSalary = this.form.salary
    },
    initDict() {
      listPost({}).then(res => {
        this.postOptions = res.rows
      })
    },
    selectDataList() {
      this.formOpen = true
      setTimeout(() => {
        this.$refs.hrEntrySelectList.getList()
      })
    }

  }

}
</script>
